/*
 * @Description:
 * @Author: Cxy
 * @Date: 2023-06-29 16:27:16
 * @LastEditors: Cxy
 * @LastEditTime: 2024-02-27 10:23:54
 * @FilePath: \blogAdmin\play\src\utils\index.ts
 */

/**
 * 对象克隆
 */
export const deepClone = (obj: any) => {
  if (obj === null) return null
  const clone = Object.assign({}, obj)
  Object.keys(clone).forEach(key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key]))
  if (Array.isArray(obj)) {
    clone.length = obj.length
    return Array.from(clone)
  }
  return clone
}

/**
 * 获取当前系统应用模式
 */
export const colorScheme = () => (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')

/**
 * px转rem
 */
export const pxToRem = (px: number | string) =>
  `${Number(String(px).replace('px', '')) / import.meta.env.VITE_FOUNT_SIZE}rem`
